package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * @author sunzhihao2
 * @date 9:59 2018/5/25
 */
public class ReadExcel {
    public static List findStockList() {
        ReadExcel obj = new ReadExcel();
        // 此处为我创建Excel路径：D:/Git/MyCrawler/stockCode.xls下
        File file = new File("D:/Git/MyCrawler/stockCodeTest.xls");
        List excelList = obj.readExcel(file);
        return excelList;
    }
    // 去读Excel的方法readExcel，该方法的入口参数为一个File对象
    public static List readExcel(File file) {
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // 每个页签创建一个Sheet对象
            Sheet sheet = wb.getSheet(0);
            // sheet.getRows()返回该页的总行数

            List innerList=new ArrayList();
            // sheet.getColumns()返回该页的总列数
            //for (int j = 0; j < sheet.getColumns(); j++) {
            for (int i = 0; i < sheet.getRows(); i++) {
                String cellinfo = sheet.getCell(0, i).getContents();
                String stockCode = null;
                if (!cellinfo.isEmpty()) {
                    stockCode = toStockCodeForNeed(cellinfo);
                    innerList.add(stockCode);
                }
            }
            //}

            return innerList;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //603283_SH_EQ 转成 603283
    public static String toStockCodeForNeed(String str){
        String[] strArray = null;
        strArray = str.split("_");
        String stockCode = strArray[0];
        return stockCode;
    }
}
